package com.culture.bootdemo.controller;


import com.culture.bootdemo.common.basevo.Result;
import com.culture.bootdemo.handler.handler3.AbstractDemoBaseHandler;
import com.culture.bootdemo.handler.handler3.BaseHandler;
import com.culture.bootdemo.handler.handler3.hasT.AbBaseHandlerT;
import com.culture.bootdemo.handler.handler3.hasT.BaseHandlerT;
import com.culture.bootdemo.handler.handler4.BaseHandler4;
import com.culture.bootdemo.model.enums.MultipleServiceEnum;
import com.culture.bootdemo.handler.handler2.MultipleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ：wen
 * @date ：Created in 2019/8/5 16:42
 * @description：: 一个service多个实现类
 */
@RestController
@Slf4j
@RequestMapping("/multiple")
public class MultipleController {

    /**
     * 两种注入方式：
     */
    //方式一：
    /* @Qualifier("multipleService1Impl")
    @Autowired
    private MultipleService multipleService;*/
    //方式二：
    @Resource(name = "multipleService1Impl")
    private MultipleService multipleService;

    /**
     * 一个service多个实现类
     * 固定获取：Qualifier
     *
     * @return
     */
    @GetMapping("handler1")
    public Result handler1() {
        String test4 = multipleService.testService("hello");
        return Result.builder().data(test4).build();
    }

    @Autowired
    private Map<String, MultipleService> serviceMap;

    /**
     * 一个service多个实现类
     * 动态获取service
     *
     * @return
     */
    @GetMapping("handler1_2")
    public Result handler1_2() {
        String serviceImplName = "multipleService2Impl";
        //获取到具体的实现类
        MultipleService service = serviceMap.get(serviceImplName);
        String test5 = service.testService("张三");
        return Result.builder().data(test5).build();
    }

    /**
     * 一个service多个实现类
     * 动态实现
     *
     * @return
     */
    @GetMapping("handler2")
    public Result handler2(String id) {
        System.out.println(serviceMap.toString());
        MultipleService service = serviceMap.get(MultipleServiceEnum.getInstanceDesc(id));
        String test6 = service.testService("赵武");
        return Result.builder().data(test6).build();
    }


    /**
     * 一个service 多个类
     *
     * @param type
     * @return
     */
    @GetMapping("handler3")
    public Result handler3(Integer type) {
        Map<Integer, BaseHandler> activityStrategyMap = AbstractDemoBaseHandler.activityStrategyMap;
        activityStrategyMap.get(type).deal();
        return Result.builder().data(0).build();
    }

    /**
     * 同test7 多了返回值
     *
     * @param type
     * @return
     */
    @GetMapping("handler3T")
    public Result handler3T(Integer type) {
        Map<Integer, BaseHandlerT> activityStrategyMap = AbBaseHandlerT.activityStrategyMap;
        Object deal = activityStrategyMap.get(type).deal();
        return Result.builder().data(deal).build();
    }

    @GetMapping("handler4")
    public Result handler4(Integer type) {
        Map<Integer, BaseHandler4> activityStrategy4Map = BaseHandler4.activityStrategy4Map;
        BaseHandler4 baseHandler4 = activityStrategy4Map.get(type);
        baseHandler4.deal();
        return Result.builder().data(1).build();
    }
}
